<script setup lang="ts">
import { uploadImg } from '@/services/consult'
import type { Image } from '@/types/consult'
import { showLoadingToast, type UploaderAfterRead } from 'vant'
import { ref } from 'vue'

defineProps<{
  disabled: boolean
}>()

const emit = defineEmits<{
  (e: 'send-text', text: string): void
  (e: 'send-image', img: Image): void
}>()

// 发送消息
const text = ref('')
const sendText = () => {
  emit('send-text', text.value)
  text.value = ''
}
// 发送图片
const sendImage: UploaderAfterRead = async (data) => {
  // 排除多图上传数组情况
  if (Array.isArray(data)) return
  if (!data.file) return
  const loading = showLoadingToast({
    message: '正在上传',
    duration: 0,
  })
  const res = await uploadImg(data.file)
  // 关闭提示
  loading.close()
  emit('send-image', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      @keyup.enter="sendText"
      v-model="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="disabled"
    ></van-field>
    <van-uploader :after-read="sendImage" :preview-image="false" :disabled="disabled">
      <svg-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  :deep() {
    .custom-svg-icon {
      width: 24px;
      height: 24px;
    }
  }
}
</style>
